package com.guyaning.deyson_ec.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.blankj.utilcode.util.RegexUtils;
import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_core.core.wechat.LatteWeChat;
import com.guyaning.deyson_core.core.wechat.callbacks.IWeChatSignInCallBack;
import com.guyaning.deyson_ec.ec.R;
import com.guyaning.deyson_ec.ec.R2;
import com.joanzapata.iconify.widget.IconTextView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/3.
 */

public class SignInDelegate extends DeysonDelegate {
    @BindView(R2.id.edit_sign_in_email)
    TextInputEditText mEmail = null;
    @BindView(R2.id.edit_sign_in_password)
    TextInputEditText mPassword = null;
    @BindView(R2.id.btn_sign_in)
    AppCompatButton btnSignIn = null;
    @BindView(R2.id.tv_link_sign_up)
    AppCompatTextView tvLinkSignUp = null;
    @BindView(R2.id.icon_sign_in_wechat)
    IconTextView iconSignInWechat = null;

    private ISignListener iSignListener = null;
    private String email;
    private String password;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof ISignListener) {
            iSignListener = (ISignListener) activity;
        }
    }

    private boolean checkForm() {

        email = mEmail.getText().toString().trim();
        password = mPassword.getText().toString().trim();

        boolean isPass = true;

        if (email.isEmpty() || !RegexUtils.isEmail(email)) {
            mEmail.setError("请输入正确的邮箱地址");
            isPass = false;
        } else {
            mEmail.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mPassword.setError("密码不能少于六位");
            isPass = false;
        } else {
            mPassword.setError(null);
        }

        return isPass;

    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_in;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

    }

    @OnClick(R2.id.btn_sign_in)
    public void onBtnSignInClicked() {
        if (checkForm()) {

            //登录验证
//            RestClient.builder()
//                    .url("")
//                    .params("email",email)
//                    .params("password",password)
//                    .loader(getContext())
//                    .success(new ISuccess() {
//                        @Override
//                        public void onSuccess(String response) {
//                            SignHandler.onSignIn(response,iSignListener);
//                        }
//                    })
//                    .build()
//                    .post();
        }
    }

    @OnClick(R2.id.tv_link_sign_up)
    public void onTvLinkSignUpClicked() {
        start(new SignUpDelegate(), SINGLETASK);
    }

    @OnClick(R2.id.icon_sign_in_wechat)
    public void onIconSignInWechatClicked() {
        LatteWeChat.getInstance().onSignInSuccess(new IWeChatSignInCallBack() {
            @Override
            public void onSignInSuccess(String userInfo) {

            }
        }).signIn();
    }
}
